import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import * as ElementPlusIconsVue from '@element-plus/icons-vue' // 导入所有图标
// 按需引入图标
import { ArrowLeft, Star, ShoppingCart } from '@element-plus/icons-vue'

axios.defaults.baseURL = 'http://127.0.0.1:8000'
axios.defaults.withCredentials = true

const app = createApp(App)

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
// 优化后的懒加载函数
const lazyLoadImages = () => {
  const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
      if (entry.isIntersecting) {
        const img = entry.target
        img.src = img.dataset.src
        observer.unobserve(img)
      }
    })
  }, { rootMargin: '0px 0px 200px 0px' })

  document.querySelectorAll('img[data-lazy]').forEach(img => {
    img.removeAttribute('loading') // 清除原生属性
    observer.observe(img)
  })
}

app.use(ElementPlus)
app.use(router)
lazyLoadImages() // 提前初始化
app.mount('#app')
